# Create your views here.

import time
import datetime
from django.shortcuts import render_to_response
  
from SkyProject.sky.models import Hill
from SkyProject.sky.models import OfficialReport
from SkyProject.sky.models import Feedback
from SkyProject.sky.models import User
from django.template.context import RequestContext


 
def hill(request):
    return render_to_response('hill.html', context_instance=RequestContext(request))

def report(request):
    hills = Hill.objects.all()
    hill = Hill()
    if hills.count() != 0 :
        hillName =request.GET.get('name')
        print hillName
        for h in hills :
            if h.name == hillName :
                hill = h
    return render_to_response('report.html',{'hill' : hill,}, context_instance=RequestContext(request))



def createHill(request):
    hillname = request.POST.get('hillName')
    print hillname
    hillNumberLifts = request.POST.get('numberLifts')
    hillNumberBlackRuns = request.POST.get('numberBlackRuns')
    hillNumberBlueRuns = request.POST.get('numberBlueRuns')
    hillNumberGreenRuns = request.POST.get('numberGreenRuns')
    hillNumberRedRuns = request.POST.get('numberRedRuns')
    hill = Hill(name = hillname, numberLifts = hillNumberLifts, numberBlackRuns = hillNumberBlackRuns, numberBlueRuns = hillNumberBlueRuns, numberGreenRuns = hillNumberGreenRuns, numberRedRuns = hillNumberRedRuns )
    hill.save()
    
def createFeedBack(request, hillName):
    feedHillName = hillName
    feedOverallSnowCondition = request.POST.get('overall')
    if feedOverallSnowCondition < 0 :
        feedOverallSnowCondition = 0
    elif feedOverallSnowCondition > 4 :
        feedOverallSnowCondition = 4
    feedEvaluation = request.POST.get('evaluation')
    feedComment = request.POST.get('comment')
    now = datetime.date.today()
    print "The date", str(now)
    feedback = Feedback(date = str(now), hillName = feedHillName, overallSnowCondition = feedOverallSnowCondition, evaluation = feedEvaluation, comment = feedComment)
    feedback.save()
    
def createReport(request,hillName):
    reportHillName = hillName
    reportSnowAtBottom = request.POST.get('snowAtBottom')
    reportSnowAtTop = request.POST.get('snowAtTop')
    reportNumberOpenLifts = request.POST.get('numberOpenLifts')
    reportNumberOpenBlackRuns = request.POST.get('numberOpenBlackRuns')
    reportNumberOpenBlueRuns = request.POST.get('numberOpenBlueRuns')
    reportNumberOpenGreenRuns = request.POST.get('numberOpenGreenRuns')
    reportNumberOpenRedRuns = request.POST.get('numberOpenRedRuns')
    report = OfficialReport(hillName = reportHillName, snowAtBottom = reportSnowAtBottom, snowAtTop = reportSnowAtTop, numberOpenLifts = reportNumberOpenLifts, numberOpenBlackRuns = reportNumberOpenBlackRuns, numberOpenBlueRuns = reportNumberOpenBlueRuns, numberOpenGreenRuns = reportNumberOpenGreenRuns, numberOpenRedRuns = reportNumberOpenRedRuns )
    report.save()
    
    
    
def home(request):
    adminLogin = "admin"  
    adminPass = "admin"   
    if request.method == 'POST' and request.POST.get('snowAtBottom') != None and request.POST.get('login') == adminLogin and request.POST.get('password') == adminPass: 
        snowBottom = -1
        snowTop = -1
        try:
            snowBottom = int(request.POST.get('snowAtBottom'))
            snowTop = int(request.POST.get('snowAtTop'))
        except ValueError:
            snowBottom = -1
            snowTop = -1
        if snowBottom > 0 and snowTop > 0 :
            createReport(request,request.GET.get('name'))
    elif request.method == 'POST' and request.POST.get('comment') != None:        
        createFeedBack(request, request.GET.get('name'))
    elif request.method == 'POST' and request.POST.get('login') == 'admin' and request.POST.get('password') == 'admin':
        createHill(request)
    hills = Hill.objects.all()
    hill = Hill()
    hillName2 = ""
    if hills.count() != 0 :
        hillName2 =request.GET.get('name')
        print hillName2
        for h in hills :
            if h.name == hillName2 :
                hill = h
                 
    report = OfficialReport()
    reports = OfficialReport.objects.all()
    if reports.count() != 0 :
        for r in reports:
            if r.hillName == hillName2 :
                report = r
                    
    feeds = Feedback.objects.filter(hillName = hillName2)
    
    hill.grade = 0
    temp = 0
    f = Feedback()
    for f in feeds:
        temp = temp + f.overallSnowCondition    
    if feeds.count() != 0:    
        hill.grade = temp/feeds.count()

    
    return render_to_response('home.html', { 
            'hills' : hills,
            'hill' : hill,
            'report' : report,
            'feedbacks' : feeds
                    }, context_instance=RequestContext(request))
   
